/**----------------------------------------
 * @description 文件工具类
 * @author      lxc
 * @file        FileTool.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */

import { LogClass } from "../LogClass";

export class FileTool extends LogClass {
    protected logPrefix: string = "FileTool";
    /**
     * 文件存储根路径
     */
    rootPath = CC_JSB ? jsb.fileUtils.getWritablePath() : "/"
    /**
     * 缓存路径
     */
    cachePath = this.rootPath + "cachePath/"
    /**
     * 日志存储路径
     */
    logPath = this.rootPath + "log/"
    /**
     * 当前游戏的 log 文件，年月日时分秒
     */
    currLogFile = null;

    constructor() {
        super()
        if (CC_JSB) {
            jsb.fileUtils.createDirectory(this.cachePath);
            jsb.fileUtils.createDirectory(this.logPath);
        }
    }
    /**
     * 获取url文件的本地存储路径
     * @param url 
     */
    getSavePath(url: string): string {
        return url && this.cachePath + 'md5(url)' + this.getFileSuffix(url);
    }
    /**
     * 判断文件是否存在
     * @param filePath 
     * @returns 
     */
    fileExists(filePath: string): boolean {
        return CC_JSB ? jsb.fileUtils.isFileExist(filePath) : false;
    }
    /**
     * 判断路径是否存在
     * @param dirPath 
     * @returns 
     */
    dirExists(dirPath: string): boolean {
        return CC_JSB ? jsb.fileUtils.isDirectoryExist(dirPath) : false;
    }
    /**
     * 获取文件后缀
     * @param filePath 
     * @returns 
     */
    getFileSuffix(filePath: string): string {
        return filePath && filePath.substring(filePath.lastIndexOf('.'));
    }
    /**
     * 获取文件路径
     * @param filePath 
     */
    getFilePath(filePath: string): string {
        return filePath && filePath.substring(0, filePath.lastIndexOf("/") + 1);
    }
    /**
     * 清理路径内容
     * @param filePath 
     */
    clearPath(filePath: string) {
        if (CC_JSB) {
            let currDir = this.getFilePath(filePath);
            if (this.fileExists(filePath)) {
                this.printLog("remove file : ", filePath);
                jsb.fileUtils.removeFile(filePath)
            }

            if (this.dirExists(filePath)) {
                this.printLog("remove dir : ", filePath)
                jsb.fileUtils.removeDirectory(filePath)
            }

            if (!this.dirExists(currDir)) {
                this.printLog("create dir : ", currDir);
                jsb.fileUtils.createDirectory(currDir);
            }
        }
    }
    /**
     * 写入日志至本地日志文件
     * @param log 
     */
    writeLocalLog(log: string) {
        if (CC_JSB) {
            if (!this.dirExists(this.logPath)) {
                this.printError("writeLocalLog logPath null")
            }
            if (this.currLogFile) {
                let yetLog = jsb.fileUtils.getStringFromFile(this.currLogFile)
                jsb.fileUtils.writeStringToFile(log + "[@]" + yetLog, this.currLogFile)
            } else {
                this.currLogFile = this.logPath + "log.txt"
                jsb.fileUtils.writeStringToFile(log, this.currLogFile)
            }
        }
    }
    /**
     * http上传本地日志文件
     */
    uploadLogFile() {

    }
    /**
     * 打印本地存储日志
     */
    printLocalLog() {
        if (CC_JSB && this.currLogFile) {
            let yetLog = jsb.fileUtils.getStringFromFile(this.currLogFile)
            this.printLog(yetLog);
        }
    }

    async readFile() {
        let file = await this.loadFile("filename")
    }

    loadFile(fileName: string) {
        let filePath = "path/" + fileName
        return new Promise((resolve, reject) => {
            cc.loader.loadRes(filePath, (err, asset: cc.Asset) => {
                if (err) {
                    reject(err)
                } else {
                    resolve(asset);
                }
            })
        })
    }

}